
package ale.android.taxiDrivers;

import ale.android.R;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;

public class EditTaxiDriver extends Activity {
	private EditText editTextName;
	private EditText editTextNick;
	private EditText editTextNationality;
	private Spinner spinnerCompany;
	private EditText editTextPhone;
	private Cursor c;

	private RatingBar ranking;

	private Button buttonEdit;
	private Button buttonCancel;
	
	private int idselected;

	SQLiteDatabase db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_taxi_driver);

		initDB();
		initViews();

	}

	private void initViews() {

		editTextName = (EditText) findViewById(R.id.editTextName);
		editTextNick = (EditText) findViewById(R.id.editTextNick);
		editTextNationality = (EditText) findViewById(R.id.editTextNationality);

		spinnerCompany = (Spinner) findViewById(R.id.spinnerCompany);
		ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
				R.array.company, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerCompany.setAdapter(adapter);

		editTextPhone = (EditText) findViewById(R.id.editTextPhone);
		ranking = (RatingBar) findViewById(R.id.ratingBar);
		

		Bundle extras = getIntent().getExtras();
		if(extras !=null)
		{
			idselected = extras.getInt("idselected");
		}
		
		String[] campos = new String[] {"Name","Nick","Nationality", "Company","Phone", "Ranking"};
		String[] args = new String []{Integer.toString(idselected)};
		c = db.query("TaxiDrivers", campos, "_id=?", args, null, null, null);
		
		if(c.moveToFirst()){
			editTextName.setText(c.getString(0));
			editTextNick.setText(c.getString(1));
			editTextNationality.setText(c.getString(2));
			String company = c.getString(3);
			if(company.equals("Taxi Barcelona SL"))spinnerCompany.setSelection(0);
			else if(company.equals("Fast Taxi SL"))spinnerCompany.setSelection(1);
			else if(company.equals("My Traveler SA"))spinnerCompany.setSelection(2);
			else if(company.equals("Airport Taxi SA"))spinnerCompany.setSelection(3);
			else if(company.equals("BonoTaxi SL"))spinnerCompany.setSelection(4);
			else spinnerCompany.setSelection(5);
			editTextPhone.setText(c.getString(4));
			float rank =(c.getInt(5)/2);
			ranking.setRating(rank);
			
		}
		else{
			Toast.makeText(getApplicationContext(), "Error",
					Toast.LENGTH_SHORT).show();
		}

		buttonEdit = (Button) findViewById(R.id.buttonEditTaxiDriver);
		buttonEdit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				if (dadesOk()) {
					db.execSQL("UPDATE TaxiDrivers SET Name ='"+
				editTextName.getText().toString()+ "', Nick ='"
					+ editTextNick.getText().toString()
					+ "', Nationality ='"
					+ editTextNationality.getText().toString()
					+ "',  Company='"
					+ spinnerCompany.getSelectedItem().toString()
					+"', Phone ='"
					+ editTextPhone.getText().toString()
					+ "' , Ranking='" + (int) ranking.getRating() * 2 + "' WHERE _id = "+idselected);
					finish();
					Toast.makeText(getApplicationContext(), "Changes done successfully",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), "Incorrect data",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		buttonCancel = (Button) findViewById(R.id.buttonCancelTaxiDriver);
		buttonCancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				finish();
			}
		});

	}

	private void initDB() {
		TaxiDriversSQLiteHelper usdbh = new TaxiDriversSQLiteHelper(this,
				"DBTaxiDrivers", null, 1);
		db = usdbh.getWritableDatabase();
	}

	private boolean dadesOk() {

		if (editTextName.getText().toString().equals(""))
			return false;
		if (editTextNick.getText().toString().equals(""))
			return false;
		if (editTextNationality.getText().toString().equals(""))
			return false;
		if (spinnerCompany.getSelectedItem().toString().equals(""))
			return false;
		if (editTextName.getText().toString().equals(""))
			return false;

		return true;
	}
}
